function [ output_args ] = Untitled( input_args )
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
 

    %vertex = bwtraceboundary(bwImage, boundary(1,:), 'W');

    %For each point on the boundary
    for i = 1: size(boundary,1)
        %if the point is a vertex
        if isVertex(i)
            %Search forward for the next vertex edge
            for j = i +1: size(boundary,1)
                 %Find all triangles with edge(i,j)
                 trisWithEdge = findTrisWithEdge(i,j, tri);
                 if ~isempty(trisWithEdge)
                     for k = 1: size(trisWithEdge,1)
                         thirdVertex = getThirdVertex(i,j,k,tri);
                         
                         if isOnRight(i,j, thirdVertex, boundary)
                             includedTri = [includedTri;triWithEdge(k,:)];
                             d1 = dist(i,thirdVertex, boundary);
                             d2 = dist(j,thirdVertex, boundary);
                             if min(d1,d2) == d1
                                 midpoint = getMidpoint(i,k);
                             else
                                 midpoint = getMidpoint(j,k);
                             end
                             
                             midpoints = [midpoints;midpoint];
                         end
                     end
                 end
            end
       end
    end
    
    function thirdVertex = getThirdVertex(m,n, triangle)
        
        f = triangle(triangle ~= m );
        g = triangle(triangle ~= n );
        thirdVertex = intersect(f,g);
    end
    
    function d = dist(j,k, boundary)
        p = boundary(j,:);
        q = boundary(j,:);
        d = sqrt( (p(1) - q(1))^2 + (p(2) - q(2))^2);
    end


    function trisWithEdge = findTrisWithEdge(j,k, tri)

        trisWithEdge = [];
        for iTri = 1: size(tri,1)
            if ismember(j, tri(iTri,:)) && ismember(k, tri(iTri,:))
                    trisWithEdge = [trisWithEdge,iTri];
            end
        end
    end

    function result = isOnRight(i,j,k, boundary)
        A = boundary(i,:);
        B = boundary(j,:);
        C = boundary(k,:);
        AB = [B(1)-A(1), B(2) - A(2), 0];
        AC = [C(1)-A(1), C(2) - A(2), 0];
        if cross(AB,AC) > 0
            result = 1;
        else
            result = 0;
        end
    end
end
                    
           
                   
       %% 
       
       % if v1 lies on the boundary and the next vertex that lies on
       % the boundary is V2, V1 and V2 form an edge, otherwise the boundary
       % will not be coverd by edges.
       %If v1 and V2 form an edge, 
       %  - at most 2 triangles, t(1,2,3) and t(1,2,4) can have it as an edge.
       % - one of the triangles must be contained within the object
       % - 
       % If vector, V(1,2) x V(1,4)
       

